package org.linlinjava.litemall.admin.controller;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.util.PageUtil;
import org.linlinjava.litemall.admin.beans.vo.HistoryVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.dao.LitemallSearchHistoryMapper;
import org.linlinjava.litemall.db.domain.LitemallSearchHistory;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallSearchHistoryService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理历史控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags = "搜索历史记录API")
@RestController
@RequestMapping("/admin/history")
public class AdminHistoryController {

    /**
     * 搜索历史记录服务
     */
    @Autowired
    private  LitemallSearchHistoryService searchHistoryService;

    /**
     * 搜索历史映射器
     */
    @Autowired
    private  LitemallSearchHistoryMapper searchHistoryMapper;

    /**
     * 用户服务
     */
    @Autowired
    private  LitemallUserService litemallUserService;

    /**
     * 列表
     *
     * @param userId  用户id
     * @param keyword 关键字
     * @param page    页面
     * @param limit   限制
     * @param sort    排序
     * @param order   订单
     * @return {@link Object}
     */
    @ApiOperation(value = "搜索历史列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID"),
            @ApiImplicitParam(name = "keyword",value = "搜索关键字"),
            @ApiImplicitParam(name = "search",value = "用户名称|邮箱号|手机号")
    })
    @GetMapping("/list")
    @LogAnno
    public Object list(String userId, String keyword,String search,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        /*List<LitemallSearchHistory> historyList = searchHistoryService.querySelective(userId, keyword, page, limit,sort, order);*/
        List<HistoryVo> historyVoList = CollUtil.newArrayList();
        if(StrUtil.isBlank(userId)&&StrUtil.isBlank(keyword)&&StrUtil.isBlank(search)){
            historyVoList = searchHistoryMapper.selectJoinList(HistoryVo.class, new MPJLambdaWrapper<LitemallSearchHistory>()
                    .selectAll(LitemallSearchHistory.class)
                    .select(LitemallUser::getNickname)
                    .select(LitemallUser::getUsername)
                    .select(LitemallUser::getMobile)
                    .select(LitemallUser::getEmail)
                    .leftJoin(LitemallUser.class, LitemallUser::getId, LitemallSearchHistory::getUserId));
        }else {
            historyVoList = searchHistoryMapper.selectJoinList(HistoryVo.class, new MPJLambdaWrapper<LitemallSearchHistory>()
                    .selectAll(LitemallSearchHistory.class)
                    .select(LitemallUser::getNickname)
                    .select(LitemallUser::getUsername)
                    .select(LitemallUser::getMobile)
                    .select(LitemallUser::getEmail)
                    .leftJoin(LitemallUser.class, LitemallUser::getId, LitemallSearchHistory::getUserId)

                    .eq(StrUtil.isNotBlank(userId), LitemallSearchHistory::getUserId, userId)
                    .eq(StrUtil.isNotBlank(keyword), LitemallSearchHistory::getKeyword, keyword)
                    .like(StrUtil.isNotBlank(search), LitemallUser::getEmail, search)
                    .or()
                    .eq(StrUtil.isNotBlank(userId), LitemallSearchHistory::getUserId, userId)
                    .eq(StrUtil.isNotBlank(keyword), LitemallSearchHistory::getKeyword, keyword)
                    .eq(StrUtil.isNotBlank(search), LitemallUser::getMobile, search)
                    .or()
                    .eq(StrUtil.isNotBlank(userId), LitemallSearchHistory::getUserId, userId)
                    .eq(StrUtil.isNotBlank(keyword), LitemallSearchHistory::getKeyword, keyword)
                    .like(StrUtil.isNotBlank(search), LitemallUser::getUsername, search)
            );
        }
        Pageable pageable = PageRequest.of(page -1 ,limit);
        return ResponseUtil.ok(PageUtil.getPageLumierePayMemberUserLogVo(pageable,historyVoList));
    }
}
